#pragma once

#include <iostream>
#include <string>
#include <pthread.h>
#include <ctime>

typedef void (*callback_t)();
static int num = 1;


// C++11 线程
class thread
{
    static void* routine(void* args)
    {
        thread* t = static_cast<thread*>(args);
        t->entry();
        return nullptr;
    }

public:
    thread(callback_t cb)
        : _tid(0)
        , _name("")
        , _timestamp(0)
        , _isrunning(false)
        , _cb(cb)
    {}
    void run()
    {
        _name = "thread-" + std::to_string(num++);
        _timestamp = time(nullptr);
        _isrunning = true;
        pthread_create(&_tid, nullptr, routine, this);
    }
    void join()
    {
        pthread_join(_tid, nullptr);
        _isrunning = false;
    }

    std::string get_name()
    {
        return _name;
    }
    bool is_running()
    {
        return _isrunning;
    }
    uint64_t get_timestamp()
    {
        return _timestamp;
    }
    void entry()
    {
        _cb();
    }

    ~thread()
    {}

private:
    pthread_t _tid;
    std::string _name;
    uint64_t _timestamp;
    bool _isrunning;

    callback_t _cb;
};
